package com.dao.impl;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

import org.hibernate.LockMode;
import org.springframework.stereotype.Repository;

import com.dao.IRetWODao;
import com.model.Ret_wo;
import com.rtnBean.NotFoundRtnBeen;
import com.rtnBean.SimpleRtnBeen;
import com.tx.Xpaplywo.XpaplywoI;
import com.tx.Xpaplywo.XpaplywoO;
import com.util.Util;

import static com.def.GenericDef.*;

@Repository
public class RetWoHibernateDao extends GenericHibernateDao<Ret_wo, String>
		implements IRetWODao {
	public XpaplywoO queryProc(XpaplywoI xpaplywoI) {
		XpaplywoO xpaplywoO = new XpaplywoO();
		return xpaplywoO;
	}

	public XpaplywoO addProc() {
		XpaplywoO xpaplywoO = new XpaplywoO();
		return xpaplywoO;
	}

	public XpaplywoO updateProc() {
		XpaplywoO xpaplywoO = new XpaplywoO();
		return xpaplywoO;
	}

	public XpaplywoO deleteProc() {
		XpaplywoO xpaplywoO = new XpaplywoO();
		return xpaplywoO;
	}

	public SimpleRtnBeen colseWo(Ret_wo ret_wo) {
		SimpleRtnBeen rtn = new SimpleRtnBeen();
		int comp_prd_qty_sum = 0;
		int wh_in_prd_qty_sum = 0;
		int wip_wh_in_prd_qty_sum = 0;
		int wh_out_prd_qty_sum = 0;
		int wip_wh_out_prd_qty_sum = 0;
		int scrp_prd_qty_sum = 0;
		Timestamp EvtTimestamp = Util.getCurrentTimestamp();
		Boolean allWoClosed = true;

		String queryWoSql = null;
		Ret_wo father_so = new Ret_wo();
		List<Ret_wo> ret_woList = new ArrayList<Ret_wo>();
	
		// Update WO
		// 内部订单
		// 出货（成品＋半成品出货（出货给客户））数量=计划数量（内部订单绑定箱子总产品数量），自动关闭
		if (!ret_wo.getWo_stat().equals(WO_STATUS_CLOS)) {
			if ((ret_wo.getWh_out_prd_qty().intValue()+ret_wo.getWh_in_prd_qty().intValue()
					+ret_wo.getWip_wh_in_prd_qty().intValue()+ret_wo.getScrp_prd_qty().intValue()) 
				== ret_wo.getPln_prd_qty()) {
				ret_wo.setWo_stat(WO_STATUS_CLOS);
				ret_wo.setAct_cmp_timestamp(EvtTimestamp);
			}
		}
 
		this.update(ret_wo);

		father_so = this.getWithLock(ret_wo.getSo_id(), LockMode.UPGRADE);
		if (null == father_so) {
			rtn.setMsgBuf(new NotFoundRtnBeen(ret_wo.getSo_id(), Ret_wo.class)
					.toString());
			rtn.setlRc(E_RET_WO + E_READ_NOT_FOUND);
			return rtn;
		}

		// Update SO
		// 商务订单
		// 所有内部订单都关闭后，商务订单数量＝所有内部订单的出货（成品＋半成品出货（出货给客户））数量之和，商务订单自动关闭
		queryWoSql = "FROM Ret_wo where wo_typ='W' and so_id ='"
				+ father_so.getWo_id() + "' ";
		ret_woList = this.find(queryWoSql);
		for (Ret_wo wo : ret_woList) {
			comp_prd_qty_sum += wo.getComp_prd_qty();
			wh_in_prd_qty_sum += wo.getWh_in_prd_qty();
			wip_wh_in_prd_qty_sum += wo.getWip_wh_in_prd_qty();
			wh_out_prd_qty_sum += wo.getWh_out_prd_qty();
			wip_wh_out_prd_qty_sum += wo.getWip_wh_out_prd_qty();
			scrp_prd_qty_sum += wo.getScrp_prd_qty();

			if (allWoClosed && !WO_STATUS_CLOS.equals(wo.getWo_stat())) {
				allWoClosed = false;
			}
		}
		father_so.setComp_prd_qty(comp_prd_qty_sum);
		father_so.setWh_in_prd_qty(wh_in_prd_qty_sum);
		father_so.setWip_wh_in_prd_qty(wip_wh_in_prd_qty_sum);
		father_so.setWh_out_prd_qty(wh_out_prd_qty_sum);
		father_so.setWip_wh_out_prd_qty(wip_wh_out_prd_qty_sum);
		father_so.setScrp_prd_qty(scrp_prd_qty_sum);

		if (allWoClosed) {
			father_so.setWo_stat(WO_STATUS_CLOS);
		}
		this.update(father_so);


//		rtn.setObj(ret_wo);
		return rtn;
	}
}
